<?php

namespace Controller\Column;

class Index extends \Controller\Base {

    public function actionIndex() {
        $categoryId = $this->getRouteParams(0);
        $categoryInfo = \Data\Category::instance()->getCategoryById($categoryId);
        if (!$categoryInfo) {
            $this->throwException('分类不存在');
        }
        if ($categoryInfo['Type'] == 2) {
            // 只有单片文章的情况
            $articleId = \Data\Article::instance()->getArticleIdByCategoryId($categoryId);
            if (!$articleId) {
                $this->throwException('文章未找到');
            }
            \Lib\Util\Comm::instance()->redirect('/Article/Detail?id=' . $articleId);
        } else if ($categoryInfo['Type'] == 0) {
            // 跳转到第一个分类下
            $firstCategoryId = \Data\Category::instance()->getFirstCategoryId($categoryId);
            if (!$firstCategoryId) {
                $this->throwException('未找到子分类');
            }
            \Lib\Util\Comm::redirect('/column/' . $firstCategoryId);
            exit;
        }
        // 获取父分类信息
        $pc = \Data\Category::instance()->getParentInfoById($categoryId);
        $pc = array_shift($pc);
        $this->setCurrentMenu(array($pc['CategoryId'], 0));
        // 自定义样式模板
        $tpl = $pc ? 'Column/' . $pc['Style'] . '/Index' : '';
        // 获取数据列表
        $pager = new \Lib\Core\Pager();
        $pager->currentPage = $this->getInt('page', 1);
        $pager->pageSize = 10;
        $articleList = \Data\Article::instance()->getList($pager, $categoryId);
        $this->assign('categoryId', $categoryId);
        $this->assign('categoryInfo', $pc);
        $this->assign('articleList', $articleList);
        $this->assign('pager', $pager);

        $this->display($tpl);
    }
}